package com.summarecon.qualitycontrol;

import com.summarecon.singleton.ColorPicker;
import com.summarecon.singleton.ImageData;
import com.summarecon.utility.DrawView;

import de.ipcas.colorPicker.ColorPickerDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class DefectMarkActivity extends Activity {

	public AlertDialog myAlertDialog;
	public Dialog colorDialog;
	DrawView myDrawView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_defect_mark);

		byte[] accImage = ImageData.getImage();
		Log.i("data", accImage.toString());
		if(accImage != null){
			ImageView image = (ImageView) findViewById(R.id.blobContainer);
			image.setImageBitmap(BitmapFactory.decodeByteArray( accImage,0,accImage.length));
		}

		RelativeLayout parent = (RelativeLayout) findViewById(R.id.drawLayer);
		myDrawView = new DrawView(this);
		parent.addView(myDrawView);
		myDrawView.setColor(0xFF000000);

		Button btnDiscard = (Button)findViewById(R.id.discardBtn);  
		btnDiscard.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				showDialog(DefectMarkActivity.this);
			}
		});

		Button btnColor = (Button)findViewById(R.id.colorBtn);  
		btnColor.setOnClickListener(new OnClickListener(){
			boolean opened = false;
			@Override
			public void onClick(View arg0) {
				if(!opened){
					opened = true;
					colorDialog = new ColorPickerDialog(DefectMarkActivity.this);
					colorDialog.show();
					colorDialog.setOnDismissListener(new OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							Log.i("COLOR", ""+ColorPicker.getColor());
							myDrawView.setColor(ColorPicker.getColor());
							opened = false;
						}
					});
				}
			}
		});

		Button btnClear = (Button)findViewById(R.id.clearBtn);  
		btnClear.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				myDrawView.clear();
			}
		});
		Button btnSave = (Button)findViewById(R.id.saveBtn);  
		btnSave.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent myIntent = new Intent(getBaseContext(), WorkTypeActivity.class);
				startActivity(myIntent);
				finish();
			}
		});
	}

	public void showDialog(Context context) {
		if( myAlertDialog != null && myAlertDialog.isShowing() ) return;

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Discard Image");
		builder.setMessage("Are you sure to discard this image, and re-take a new one?");
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int arg1) {
				dialog.dismiss();
				Intent myIntent = new Intent(getBaseContext(), CameraDefectActivity.class);
				startActivity(myIntent);
				finish();
			}});
		builder.setCancelable(false);
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int arg1) {
				dialog.dismiss();
			}});
		builder.setCancelable(false);
		myAlertDialog = builder.create();
		myAlertDialog.show();
	}
}
